﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TuxedoLib;

namespace SpaceSquirrel
{
	public class PasswordScene : GameSceneBase
	{
		private static readonly string[] PASSWORD_BLOCK = new string[] { 
			"123456789",
			"0ABCDEFGH",
			"IJKLMNOPQ",
			"RSTUVWXYZ"
		};

		private int row = 0;
		private int col = 0;
		private int blinkCounter = 0;
		private string current = "";

		public PasswordScene()
			: base()
		{ }

		public override void ProcessInput(List<MyEvent> events, Dictionary<string, bool> pressedActions)
		{
			int width = PASSWORD_BLOCK[0].Length;
			int height = PASSWORD_BLOCK.Length;

			foreach (MyEvent ev in events)
			{
				int dx = 0;
				int dy = 0;
				if (ev.Down)
				{
					if (ev.Action == "up")
					{
						dy -= 1;
					}
					else if (ev.Action == "down")
					{
						dy += 1;
					}
					else if (ev.Action == "left")
					{
						dx -= 1;
					}
					else if (ev.Action == "right")
					{
						dx += 1;
					}
					else if (ev.Action == "A" || ev.Action == "start")
					{
						if (this.row == 4 && this.col >= 5)
						{
							this.Next = new TitleScene();
						}
						else
						{
							if (this.row == 4)
							{
								this.TryCommit();
							}
							else
							{
								if (this.current.Length == 4)
								{
									SoundManager.PlayNoise("head_bonk");
								}
								else
								{
									if (this.row < 4)
									{
										if (this.current.Length == 4)
										{
											SoundManager.PlayNoise("head_bonk");
										}
										else
										{
											SoundManager.PlayNoise("password_enter_digit");
											this.current += PASSWORD_BLOCK[this.row][this.col];
										}
									}
								}
							}
						}
					}
					else if (ev.Action == "B")
					{
						if (this.current.Length > 0)
						{
							this.current = this.current.Substring(this.current.Length - 1);
						}
						else
						{
							SoundManager.PlayNoise("head_bonk");
						}
					}
				}

				int newCol = this.col + dx;
				int newRow = this.row + dy;
				if (dx != 0 || dy != 0)
				{
					bool bonk = false;
					if (this.row == 4)
					{
						if (dy == -1)
						{
							// allow
						}
						else if (dy == 1)
						{
							bonk = true;
						}
						else if (dx == -1)
						{
							if (this.col >= 5)
							{
								newCol = 0;
							}
							else
							{
								bonk = true;
							}
						}
						else
						{
							if (newCol < 0 || newRow < 0)
							{
								bonk = true;
							}
							if (newCol >= width)
							{
								bonk = true;
							}
						}

						if (!bonk)
						{
							this.col = newCol;
							this.row = newRow;
							SoundManager.PlayNoise("menu_move");
						}
						else
						{
							SoundManager.PlayNoise("head_bonk");
						}
					}
				}
			}
		}

		public void TryCommit()
		{
			char c;
			if (this.current.Length != 4)
			{
				SoundManager.PlayNoise("bad_password");
				return;
			}

			foreach (int i in new int[] { 0, 1, 3 })
			{
				c = this.current[i];
				if (c < 'A' || c > 'Z')
				{
					SoundManager.PlayNoise("bad_password");
					return;
				}
			}

			c = this.current[2];
			if (c < '0' || c > '9')
			{
				SoundManager.PlayNoise("bad_password");
				return;
			}

			Context context = new Context();
			context.ConvertFromPassword(this.current);

			this.Next = new PlayScene("ship_1", 8, 9, context);
		}

		public override void Update()
		{
			this.blinkCounter++;
			JukeBox.EnsureSong("password");
		}

		private int[] GetCoord(int col, int row)
		{
			return new int[] { 8 + col * 24 + 24, 64 + row * 16 };
		}

		public override void Render(TuxedoLib.Image screen, int renderCounter)
		{
			screen.Fill(0, 0, 0);

			int hleft = 48;
			SquirrelUtil.DrawBox(screen, hleft, 0, 18, 5);
			Image header = Text.Render(-1, "Password Entry");
			screen.Blit(header, hleft + 16, 16);

			int gleft = 8;
			int gtop = 48;
			SquirrelUtil.DrawBox(screen, gleft, gtop, 30, 13);
			bool blink = this.blinkCounter % 16 < 8;
			int height = PASSWORD_BLOCK.Length;
			int width = PASSWORD_BLOCK[0].Length;
			int x, y;
			for (y = 0; y < height; ++y)
			{
				for (x = 0; x < width; ++x)
				{
					Image letter = Text.Render(-1, "" + PASSWORD_BLOCK[y][x]);
					int[] c = this.GetCoord(x, y);
					screen.Blit(letter, c[0], c[1]);
					if (this.col == x && this.row == y)
					{
						if (blink)
						{
							screen.Blit(Text.Render(-1, ">"), c[0] - 8, c[1]);
						}
					}
				}
			}

			x = 0;
			y = 4;
			int px = gleft + 24 + x * 24;
			int py = gtop + 16 + y * 16;
			screen.Blit(Text.Render(-1, "DONE"), px, py);
			if (this.row == 4 && this.col < 5 && blink)
			{
				screen.Blit(Text.Render(-1, ">"), px - 8, py);
			}
			x = 6;
			px = gleft + 24 + x * 24;
			screen.Blit(Text.Render(-1, "CANCEL"), px, py);
			if (this.row == 4 && this.col >= 5 && blink)
			{
				screen.Blit(Text.Render(-1, ">"), px - 8, py);
			}

			int oleft = 88;
			int otop = 224 - 64;
			SquirrelUtil.DrawBox(screen, oleft, otop, 8, 5);
			Image txt = Text.Render(-1, this.current);
			screen.Blit(txt, oleft + 16, otop + 16);
		}
		
	}
}
